var app = getApp()
Page({
  data: {
    list:[],
    leftBtn:[],
    isUpload:true,
    ifJoin:false,
    isMgAdd:false,
    isStock:false,
    isChangePrice:false,
    ifJoin:true,
    trash:false,
    leftCurrentId:0,
    goodsType:'',
    page:2,
    searchKey:''
  },
  onLoad: function (options) {
    if (options.type==0) {          //我的库存
        this.setData({
          isUpload:false,
          ifJoin:true,
          isStock:true,
          isMgAdd:true,
          stockType:options.type
        })
      }else if (options.type==1) {    //我的商品
        this.setData({
          ifJoin:false,
          trash:true,
          stockType:options.type
        })
      }else{                          //修改价格
        this.setData({
          isUpload:false,
          ifJoin:false,
          isStock:true,
          isChangePrice:true,
          stockType:options.type
        })
      }
    console.log(options.type)
    app.getMemInfo().then(res=>{
      this.setData({
        agentId:res.data.agentId
      })
      this.getProductTypeList(res.data.agentId)
      this.getGoodsList('1','')
    })
  },
  onReachBottom(){
    let page = this.data.page
    this.getGoodsList(page,this.data.searchKey)
    page++
    this.setData({
      page
    })
  },
  searchInput(e){
    let searchKey = e.detail.value
    this.setData({
      searchKey
    })
  },
  searchConfirm(){
    this.getGoodsList(1,this.data.searchKey)
  },
  bindLeftBtn(e){
    let type = e.currentTarget.dataset.type
    this.setData({
      leftCurrentId:e.currentTarget.dataset.id,
      goodsType:type,
      page:2
    })
    this.getGoodsList('1',this.data.searchKey)
  },
  getProductTypeList(){
    let that = this
    let leftBtn = []
    let goodsType=''
    let data = {
      agentId:this.data.agentId,
      requestType:'client'
    }
    console.log(data)
    app.request('wx_app/mall/product/type/list',data,'GET').then(res=>{
      console.log(res)
      leftBtn.push({name:'全部',id:''})
      res.data.product_types.forEach(item=>{
        if (item.parent_id==0) {
          if (this.data.stockType==0) {
            if(item.id!=1&&item.id!=98){
              leftBtn.push({name:item.name,id:item.id})
            }
          }else{
            if(item.id!=1){
              leftBtn.push({name:item.name,id:item.id})
            }
          }
        }
      })
      that.setData({
        leftBtn
      })
      goodsType = that.data.leftBtn[0].id
      that.setData({
        goodsType,
        page:2
      })
    })
  },
  getGoodsList(pageNum,searchKey){
    wx.showLoading({
      title:'正在加载'
    })
    this.setData({
      list:[]
    })
    var that = this
    var data = {
      agentId:this.data.agentId,
      type:this.data.goodsType,
      pageNum,
      searchKey,
      pageSize:10
    }
    console.log(data)
    let list = this.data.list
    app.request('wx_app/agent/stock/product/list',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        if (res.data.pageContent!='') {
          res.data.pageContent.forEach(item=>{
            list.push({
              name:item.product_name,
              price:item.price,
              stock:item.stock,
              url:item.main_img_url,
              id:item.id,
              secondTypeId:item.product_type,
              color:item.tagColor,
              companyName:item.productTag
            })
            that.setData({
              list
            })
            wx.hideLoading()
          })
        }else{
          wx.hideLoading()
          app.errorMsg('没有搜到更多物品')
        }
      }else{
        wx.hideLoading()
        app.errorMsg('没有搜到更多物品')
      }
    })
  },
  bindDelete(e){
    let data = {
      skuId:e.currentTarget.dataset.id
    }
    console.log(e)
    var that = this
    wx.showModal({
      title:'删除商品',
      content:'删除商品后，对应库存信息也被删除！',
      confirmColor:'#539FFF',
      success(res){
        if (res.confirm) {
          console.log('用户点击确定')
          app.request('wx_app/agent/stock/del',data,'GET').then(res=>{
            console.log(res)
            if(res.code==1){
              that.getGoodsList()
            }else{
              app.errorMsg(res.message)
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')

        }
      }
    })
    
    console.log(data)
    
  },
  bindStockGoodSet(e){
    console.log(e)
    wx.navigateTo({
      url: '../stockGoodsSet/stockGoodsSet?id='+e.currentTarget.dataset.id+'&type='+e.currentTarget.dataset.searchType+'&price='+e.currentTarget.dataset.price+'&stockType=2'
    })
  },
  addUnderLineGoods(e){
    console.log(e)
    wx.navigateTo({
      url:'../stockGoodsSet/stockGoodsSet?id='+e.currentTarget.dataset.id+'&type='+e.currentTarget.dataset.searchType
    })
  },
  loadMore(){
    wx.showLoading({
      title:'正在加载'
    })
    let page = this.data.page
    var that = this
    var data = {
      agentId:this.data.agentId,
      type:this.data.goodsType,
      pageNum:page,
      searchKey:this.data.searchKey
    }
    console.log(data)
    let list = this.data.list
    app.request('wx_app/agent/stock/product/list',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {      
        res.data.pageContent.forEach(item=>{
          list.push({
            name:item.product_name,
            price:item.price,
            stock:item.stock,
            url:item.main_img_url,
            id:item.id,
            secondTypeId:item.product_type,
            color:item.tagColor,
            companyName:item.productTag
          })
          that.setData({
            list
          })
        })
        wx.hideLoading()
      }else{
        wx.hideLoading()
        res.errorMsg(res.message)
      }
    })
    page++
    this.setData({
      page:page
    })
  },
})